package cn.maihe.elg.operation.web.business;

import cn.maihe.elg.operation.exception.ErrorCode;
import cn.maihe.elg.operation.exception.SystemRuntimeException;
import cn.maihe.elg.operation.model.enums.InvoiceSetFlagEnum;
import cn.maihe.elg.operation.model.enums.UserType;
import cn.maihe.elg.operation.model.vo.business.UpdateOrgInstitutionSetVO;
import cn.maihe.elg.operation.repository.entity.*;
import cn.maihe.elg.operation.security.model.UserContext;
import cn.maihe.elg.operation.service.business.OrgInstitutionSetService;
import cn.maihe.elg.operation.utils.UserContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description 出函机构设置controller
 * @Author WJH
 * @Date 2021/03/25
 */
@Api(value = "出函机构设置管理", tags = "出函机构设置管理")
@RestController
@RequestMapping("/api")
@Slf4j
public class OrgInstitutionSetController {
    @Resource
    private OrgInstitutionSetService orgInstitutionSetService;


    @ApiOperation(value = "获取出函机构设置", notes = "根据出函机构id获取出函机构设置")
    @ApiImplicitParam(name = "institutionId", value = "出函机构id", dataType = "long", paramType = "path", required = true)
    @RequestMapping(value = "/orgInstitutionSet/{institutionId}", method = RequestMethod.GET)
    public OrgInstitutionSetDO getInstitutionSet(@PathVariable Long institutionId) {

        UserContext uc= UserContextUtil.getUserContext();
        if(UserType.ORG.getType() != uc.getUserType()){
            throw new SystemRuntimeException(ErrorCode.AUTHERROR);
        }
        OrgInstitutionSetDO curDo = orgInstitutionSetService.selectOne(OrgInstitutionSetDOCondition.builder()
        .andOrgIdEq(uc.getOrgId()).andInstitutionIdEq(institutionId).build());

        if (curDo == null) {
            curDo = new OrgInstitutionSetDO();
            curDo.setOrgId(uc.getOrgId());
            curDo.setInstitutionId(institutionId);
            curDo.setInvoiceFlag(InvoiceSetFlagEnum.SELF.getValue());
        }
        return curDo;
    }

    @ApiOperation(value = "修改出函机构设置", notes = "根据出函机构id修改出函机构设置")
    @RequestMapping(value = "/orgInstitutionSet", method = RequestMethod.PUT)
    public void modifyInstitution(@Validated @RequestBody UpdateOrgInstitutionSetVO vo) {

        UserContext uc= UserContextUtil.getUserContext();
        if(UserType.ORG.getType() != uc.getUserType()){
            throw new SystemRuntimeException(ErrorCode.AUTHERROR);
        }
        OrgInstitutionSetDO mDO = orgInstitutionSetService.selectOne(OrgInstitutionSetDOCondition.builder()
                .andOrgIdEq(uc.getOrgId()).andInstitutionIdEq(vo.getInstitutionId()).build());
        if(mDO==null){
            mDO = new OrgInstitutionSetDO();
            BeanUtils.copyProperties(vo, mDO);
            mDO.setOrgId(uc.getOrgId());
            orgInstitutionSetService.insert(mDO);
        }else{
            BeanUtils.copyProperties(vo, mDO);
            orgInstitutionSetService.updateById(mDO);
        }
    }



}
